#!/usr/bin/env python

from resources.lists import *
from datetime import datetime
from os import listdir
import re
#import pdb


VERSION = 0.05


class MorgueFile:
    def __init__(self, file_name):
        self.raw_data = open(file_name, 'r').readlines()

    def find_line_number(self, data_to_search, string_to_find):
        line_counter = 0
        for line in data_to_search:
            #pdb.set_trace()
            if string_to_find in line:
                return line_counter
                break
            line_counter += 1

    def gold(self):
        gold_line = self.find_line_number(self.raw_data, 'Gold')
        gold_string = self.raw_data[gold_line].rstrip()
        gold_string = gold_string[5:]
        gold = int(gold_string.split(' ')[0])
        return gold

    def turns(self):
        turns_line = self.find_line_number(self.raw_data, 'Turns')
        turns_string = self.raw_data[turns_line].rstrip()
        turns_string = turns_string.split(': ')[1]
        turns = int(turns_string.split(', ')[0])
        return turns


class Morgue:
    def __init__(self, morgue_directory):
        all_files = listdir(morgue_directory)
        self.character_list = self.populate_character_list(all_files)

    def populate_character_list(self, all_files):
        character_list = []
        for file_name in all_files:
            if file_name.startswith('morgue') and file_name.endswith('.txt'):
                character_list.append(MorgueFile(file_name))
        return character_list

    def character_count(self):
        character_count = len(self.character_list)
        if character_count == 1:
            number = ' character.'
        else:
            number = ' characters.'
        return 'Your morgue contains ' + str(character_count) + number

    def total_gold(self):
        total_gold = 0
        for character in self.character_list:
            total_gold += character.gold()
        return 'Total gold acquired: ' + str(total_gold)

    def turns_summary(self):
        total_turns = 0
        for character in self.character_list:
            total_turns += character.turns()
        return 'Total turns taken: ' + str(total_turns)


class LogFile:
    def __init__(self):
        self.log_file = open('Morgue Autopsy ' + self.clean_date() +
            '.txt', 'w')
        self.log_file.write(self.log_file_header())

    def clean_date(self):
        current_time = str(datetime.now())
        date_string = re.sub("\D", "", current_time)
        return date_string

    def log_file_header(self):
        header_string = ('---Autopsy v' + str(VERSION) +
            ' a Dungeon Crawl: Stone Soup Morgue Analyzer---')
        return header_string

    def add(self, text_to_write):
        self.log_file.write('\n\n')
        self.log_file.write(text_to_write)

    def close(self):
        self.log_file.close()


def main():
    my_morgue = Morgue('.')
    log = LogFile()
    #---
    log.add(my_morgue.character_count())
    log.add(my_morgue.total_gold())
    log.add(my_morgue.turns_summary())
    #---
    log.close()

if __name__ == "__main__":
    main()
